package com.hns.phone.energy.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.vo.EnergyRankingVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.phone.energy.service.PhoneCarEnergyService;
import com.hns.phone.energy.vo.CarAnaCarVo;
import com.hns.phone.energy.vo.CarEnergyRankVo;
import com.hns.phone.energy.vo.CarEnergyVo;
import com.hns.phone.energy.vo.NewEnergy;
import com.hns.tool.pub.PubUtil;

@Controller
@RequestMapping("/api/phoneCarEnergy")
public class PhoneCarEnergyController extends BaseController {
	@SuppressWarnings("serial")
	private Map<String, String> map = new HashMap<String, String>(){
		{
			put("oil", "单车百公里油耗趋势");
			put("elec", "单车百公里电耗趋势");
			put("gas", "单车百公里气耗趋势");
		}
	};
	
	
	
	
	@SuppressWarnings("serial")
	private Map<String, String> rankMap = new HashMap<String, String>(){
		{
			put("oil", "'1', '2', '6'");//未知能耗默认归为油耗
			put("elec", "'3'");
			put("gas", "'4', '5'");
		}
	};
	
	@SuppressWarnings("serial")
	private Map<String, String> lineTrendMap = new HashMap<String, String>(){
		{
			put("oil", "030401");
			put("elec", "030402");
			put("gas", "030403");
		}
	};
	
	@Autowired
    protected CommonInitParamService commonInitParamService;
	
	@Autowired
	private PhoneCarEnergyService phoneCarEnergyService;
	
	/**
	 * 查询 能耗数据
	 * @param vo
	 * @return
	 * @throws Exception
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
    @RequestMapping("/getCarAnaEnerIdx")
    @ResponseBody
    public Object getCarAnaEnerIdx(CarAnaCarVo vo) throws Exception {
        List<CarEnergyVo> list = phoneCarEnergyService.getCarAnaIdx(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 能耗趋势图
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getCarAnaEnerTrend")
    @ResponseBody
    public Object getCarAnaEnerTrend(CarAnaCarVo vo) throws Exception {
    	List list= null;
    	if(PubUtil.isNotEmpty(vo.getCarId())){
    		String type = map.get(vo.getEnerType());
    		vo.setEnerType(type);
    		list = phoneCarEnergyService.getCarAnaTrend(vo);
    	}else{
    		vo.setUserid(this.getPhoneCurrentUser().getId());
    		String type = lineTrendMap.get(vo.getEnerType());
    		vo.setEnerType(type);
    		list = phoneCarEnergyService.getLineAnaTrend(vo);
    	}
        return this.getReturnResult(list);
    }
    
    /**
     * 车辆能耗查询
     * @param vo
     * @param response
     * @param request
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/getCarEnergy")
    @ResponseBody
    public Object searchCarEnergy(NewEnergy vo) throws Exception {
        SysUserEntity user = this.getPhoneCurrentUser();
        vo.setCorpId(user.getCorpId());
        vo.setDriverRelTableName("T_COMM_ENER_CONS_STAT_INFO");
        String enerType = vo.getEnerType();
        String fuelType = rankMap.get(enerType);
        vo.setFuelTypeCode(fuelType);
        commonInitParamService.dateType2DateTime(vo, vo.getTimeType());
        Pagination page = phoneCarEnergyService.searchCarEnergy(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 单车能耗排名
     * @param vo
     * @param response
     * @param request
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/getSoloCarEnergyRank")
    @ResponseBody
    public Object searchSoloCarEnergyRank(NewEnergy vo) throws Exception {
        SysUserEntity user = this.getPhoneCurrentUser();
        vo.setCorpId(user.getCorpId());
        vo.setDriverRelTableName("T_COMM_ENER_CONS_STAT_INFO");
        String enerType = vo.getEnerType();
        String fuelType = rankMap.get(enerType);
        vo.setFuelTypeCode(fuelType);
        commonInitParamService.dateType2DateTime(vo, vo.getTimeType());
        List<CarEnergyRankVo> list = phoneCarEnergyService.searchSoloCarEnergyRank(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 车辆能耗查询（前五）
     * @param vo
     * @param response
     * @param request
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/searchCarEnergyForFive")
    @ResponseBody
    public Object searchCarEnergyForFive(NewEnergy vo) throws Exception {
    	if(PubUtil.isNotEmpty(vo.getCarId())){
    		if(vo.getEnerType().equals("gas")){
    			vo.setEnerType("040803");
    		}else if(vo.getEnerType().equals("elec")){
    			vo.setEnerType("040802");
    		}else if(vo.getEnerType().equals("oil")){
    			vo.setEnerType("040801");
    		}
    	}else{
    		if(vo.getEnerType().equals("gas")){
    			vo.setEnerType("030701");
    		}else if(vo.getEnerType().equals("elec")){
    			vo.setEnerType("030601");
    		}else if(vo.getEnerType().equals("oil")){
    			vo.setEnerType("030501");
    		}
    	}
        SysUserEntity user = this.getPhoneCurrentUser();
        vo.setUserId(user.getId());
        List<CarEnergyRankVo> list = phoneCarEnergyService.searchCarEnergyForFive(vo);
        return this.getReturnResult(list);
    }
    
    
    
}
